home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxsync.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  4.3 KB  |  140 lines

  1. /* Copyright (C) 1998 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxsync.c,v 1.2 2000/09/19 19:00:40 lpd Exp $ */
  20. /* Interface to platform-based synchronization primitives */
  21.  
  22. /* Initial version 2/1/98 by John Desrosiers (soho@crl.com) */
  23.  
  24. #include "memory_.h"
  25. #include "gx.h"
  26. #include "gserrors.h"
  27. #include "gsmemory.h"
  28. #include "gxsync.h"
  29.  
  30. /* This module abstracts the platform-specific synchronization primitives. */
  31. /* Since these routines will see heavy use, performance is important. */
  32.  
  33.  
  34. /* ----- Semaphore interface ----- */
  35. /* These have the usual queued, counting semaphore semantics: at init time, */
  36. /* the event count is set to 0 ('wait' will wait until 1st signal). */
  37.  
  38. /* Allocate & initialize a semaphore */
  39. gx_semaphore_t *        /* returns a new semaphore, 0 if error */
  40. gx_semaphore_alloc(
  41.               gs_memory_t * memory    /* memory allocator to use */
  42. )
  43. {
  44.     gx_semaphore_t *sema;
  45.  
  46.     /* sizeof decl'd sema struct, minus semaphore placeholder's size, + actual semaphore size */
  47.     unsigned semaSizeof
  48.     = sizeof(*sema) - sizeof(sema->native) + gp_semaphore_sizeof();
  49.  
  50.     if (gp_semaphore_open(0) == 0)    /* see if gp_semaphores are movable */
  51.     /* movable */
  52.     sema = (gx_semaphore_t *) gs_alloc_bytes(memory, semaSizeof,
  53.                          "gx_semaphore (create)");
  54.     else
  55.     /* unmovable */
  56.     sema = (gx_semaphore_t *) gs_alloc_bytes_immovable(memory, semaSizeof,
  57.                            "gx_semaphore (create)");
  58.     if (sema == 0)
  59.     return 0;
  60.  
  61.     /* Make sema remember which allocator was used to allocate it */
  62.     sema->memory = memory;
  63.  
  64.     if (gp_semaphore_open(&sema->native) < 0) {
  65.     gs_free_object(memory, sema, "gx_semaphore (alloc)");
  66.     return 0;
  67.     }
  68.     return sema;
  69. }
  70.  
  71. /* Deinit & free a semaphore */
  72. void
  73. gx_semaphore_free(
  74.              gx_semaphore_t * sema    /* semaphore to delete */
  75. )
  76. {
  77.     if (sema) {
  78.     gp_semaphore_close(&sema->native);
  79.     gs_free_object(sema->memory, sema, "gx_semaphore (free)");
  80.     }
  81. }
  82.  
  83. /* Macros defined in gxsync.h, but redefined here so compiler chex consistency */
  84. #define gx_semaphore_wait(sema)  gp_semaphore_wait(&(sema)->native)
  85. #define gx_semaphore_signal(sema)  gp_semaphore_signal(&(sema)->native)
  86.  
  87.  
  88. /* ----- Monitor interface ----- */
  89. /* These have the usual monitor semantics: at init time, */
  90. /* the event count is set to 1 (1st 'enter' succeeds immediately). */
  91.  
  92. /* Allocate & Init a monitor */
  93. gx_monitor_t *            /* returns a new monitor, 0 if error */
  94. gx_monitor_alloc(
  95.             gs_memory_t * memory    /* memory allocator to use */
  96. )
  97. {
  98.     gx_monitor_t *mon;
  99.  
  100.     /* sizeof decl'd mon struct, minus monitor placeholder's size, + actual monitor size */
  101.     unsigned monSizeof
  102.     = sizeof(*mon) - sizeof(mon->native) + gp_monitor_sizeof();
  103.  
  104.     if (gp_monitor_open(0) == 0)    /* see if gp_monitors are movable */
  105.     /* movable */
  106.     mon = (gx_monitor_t *) gs_alloc_bytes(memory, monSizeof,
  107.                           "gx_monitor (create)");
  108.     else
  109.     /* unmovable */
  110.     mon = (gx_monitor_t *) gs_alloc_bytes_immovable(memory, monSizeof,
  111.                              "gx_monitor (create)");
  112.     if (mon == 0)
  113.     return 0;
  114.  
  115.     /* Make monitor remember which allocator was used to allocate it */
  116.     mon->memory = memory;
  117.  
  118.     if (gp_monitor_open(&mon->native) < 0) {
  119.     gs_free_object(memory, mon, "gx_monitor (alloc)");
  120.     return 0;
  121.     }
  122.     return mon;
  123. }
  124.  
  125. /* Dnit & free a monitor */
  126. void
  127. gx_monitor_free(
  128.            gx_monitor_t * mon    /* monitor to delete */
  129. )
  130. {
  131.     if (mon) {
  132.     gp_monitor_close(&mon->native);
  133.     gs_free_object(mon->memory, mon, "gx_monitor (free)");
  134.     }
  135. }
  136.  
  137. /* Macros defined in gxsync.h, but redefined here so compiler chex consistency */
  138. #define gx_monitor_enter(sema)  gp_monitor_enter(&(sema)->native)
  139. #define gx_monitor_leave(sema)  gp_monitor_leave(&(sema)->native)
  140.